import java.util.Arrays;

/**
 * @User: vitobo
 * @Date: 2024-04-23
 * @Description: 数组: 是一块连续的存储空间, 存储的是相同数据类型的元素
 */
public class Array2 {

    // 二维数组, 其实是 特殊的一维数组
    public static void main(String[] args) {
        // 不规则的二维数组
        int[][] arr1 = new int[2][];
        arr1[0] = new int[3];
        arr1[1] = new int[4];

        System.out.println(Arrays.deepToString(arr1));

        System.out.println(arr1[0]);
        System.out.println(arr1[1]);
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
                System.out.print(arr1[i][j]+" ");
            }
            System.out.println();
        }
    }



    public static void main4(String[] args) {
        int[][] arr1= {{1,2,3},{4,5,6},{7,8,9}};
        int[][] arr2 = new int[3][4];
        int[][] arr3 = new int[][]{{12,34},{56,78}};

        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
                System.out.print(arr1[i][j]+" ");
            }
            System.out.println();
        }

        // 输出地址,
        System.out.println(Arrays.toString(arr3));
        // 二维数组打印
        System.out.println(Arrays.deepToString(arr3));

        for (int[] arr : arr1) {
            for (int x : arr) {
                System.out.print(x + " ");
            }
            System.out.println();
        }
    }


    // 冒泡排序
    public static void bubbleSort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            boolean flg = false;   // 检查某一趟之后, 是否有序
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j] > arr[j+1]){
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;

                    flg = true; // 发生交换
                }
            }
            if(flg == false){
                return ;   // 未发生交换, 直接进入下一轮比较
            }

        }
    }

    public static void main3(String[] args) {
        int[] arr = {6, 10, 3, 5, 7, 2, 1};
        bubbleSort(arr);
        String ret = Arrays.toString(arr);
        System.out.println(ret);
    }



    // 数组逆置
    public static void reverse(int[] array){
        int left = 0;
        int right = array.length-1;
        while(left < right){
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;

            left++;
            right--;
        }
    }

    // 二分查找(有序 情况下)
    public static int binarySearch(int[] array, int key){

        int left = 0;
        int right = array.length - 1;

        while(left <= right){
            int mid = (left + right) / 2;
            if(array[mid] < key){
                left = mid + 1;
            }else if(array[mid] > key){
                right = mid - 1;
            }else {
                return mid;
            }
        }

        return -1;
    }


    // 查找数组中指定元素(顺序查找)
    public static int find(int[] array, int key){
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key){
                return i;
            }
        }
        return -1;
    }

    // 求数组中元素的平均值
    public static double avg(int[] array){
        int sum = 0;
        for (int x:
             array) {
            sum += x;
        }
        return sum*1.0 / array.length;
    }



    // 数组练习
    // 2. 数组拷贝  有原内容, 产生一个一模一样的
    public static void main2(String[] args) {
        int[] array = {1,3,5,7,9};

        int[] ret = array.clone();

        // 扩容2倍
        int[] array2 = Arrays.copyOf(array, 2*array.length);
        System.out.println(Arrays.toString(array2));


        int[] tmp = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            tmp[i] = array[i];
        }
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(tmp));

    }


    // 1. 数组转为字符串
    public static String myToString(int[] arr){
        if(arr == null){
            return "null";
        }
        String ret = "[";
        for (int i = 0; i < arr.length; i++) {
            ret += arr[i];
            if(i != arr.length-1){
                ret += ",";
            }
        }
        ret +="]";
        return ret;
    }

    public static void main1(String[] args) {
        // 整体初始化, 只有一次机会, 就是在定义的同时进行初始化
        // 聚合类型(定义的同时进行初始化)
        int[] array = {1,2,3,4};
//        String ret = Arrays.toString(array);
//        System.out.println(ret);
        String ret = myToString(array);
        System.out.println(ret);

    }


}
